document.ready(function() {
    setValue('roleid', getUrlParam('roleid'));
    setValue('roleName', getUrlParam('rolename'));
    setHtml('title', '配置操作项【所属角色：' + getUrlParam('rolename') + '】');

    getRoleOperate();
});

function getRoleOperate() {
    openLoad();

    var param = {};
    param.ctrl = 'account';
    param.func = 'getRoleOperate';
    param.roleid = getAjaxValue('roleid');

    sendAjax(param, function(result) {
        operate.list = result.operate;
        operate.ownerMenu = result.ownerMenu;
        operate.ownerRole = result.ownerRole;

        setDisplay('operateBar', '');
        closeLoad();
    });
}

function setOperateItem(event, sign, operateid, menuid, code, name) {
    var flag = '';
    if (getClass('operateItem' + operateid) == 'operate-item') {
        setClass('operateItem' + operateid, 'operate-item-select');
        flag = 'add';
    }
    else {
        setClass('operateItem' + operateid, 'operate-item');
        flag = 'delete';
    }

    var obj = sign == 'vue' ? event.target : event;
    var parentObj = obj.parentNode.parentNode;
    var operateListBar = parentObj.children[2];
    var operateList = operateListBar.children;

    if (flag == 'add') {
        var input1 = document.createElement('input');
        input1.id = 'operateid' + operateid;
        input1.type = 'text';
        input1.value = operateid;

        var input2 = document.createElement('input');
        input2.id = 'menuid' + operateid;
        input2.type = 'text';
        input2.value = menuid;

        var input3 = document.createElement('input');
        input3.id = 'code' + operateid;
        input3.type = 'text';
        input3.value = code;

        var input4 = document.createElement('input');
        input4.id = 'name' + operateid;
        input4.type = 'text';
        input4.value = name;

        var div = document.createElement('div');
        div.id = 'operateInfo' + operateid;
        div.appendChild(input1);
        div.appendChild(input2);
        div.appendChild(input3);
        div.appendChild(input4);

        operateListBar.appendChild(div);
    }
    else {
        for (var i = 0; i < operateList.length; i++) {
            if (operateList[i].id == 'operateInfo' + operateid) {
                operateListBar.removeChild(operateList[i]);
                break;
            }
        }
    }
}

function saveRoleOperate() {
    var elements = document.querySelectorAll('[id^="operateInfo"]');
    var operateList = [], operateJson = [];
    for (var i = 0; i < elements.length; i++) {
        var item = {};
        item.operateid = elements[i].children[0].value;
        item.menuid = elements[i].children[1].value;
        item.code = elements[i].children[2].value;
        item.name = elements[i].children[3].value;
        operateList.push(item);
    }
    operateJson.push(operateList);

    var param = {};
    param.ctrl = 'account';
    param.func = 'saveRoleOperate';
    param.roleid = getAjaxValue('roleid');
    param.operateList = JSON.stringify(operateJson);

    sendAjax(param, function(result) {
        back();
    });
}

function back() {
    parent.closeSlipIframeDiv();
}